/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.example.app.activity;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

/**
 * @version 2010年11月8日22时45分47秒
 * */
public class VisitUrlAct extends Activity {
	/**
	 * 值为true时，使用传统方法让用户选择。<br/>
	 * 值为false时，程序自动为用户选定浏览器浏览。<br/>
	 * 目前支持且优先级从高到低为：<br/>
	 * 1.UC浏览器<br/>
	 * 2.Opera浏览器<br/>
	 * 3.QQ浏览器<br/>
	 * 4.Dolphin Browser(不支持WAP)<br/>
	 * 5.Skyfire Browser(不支持WAP)<br/>
	 * 6.Steel Browser(不支持WAP)<br/>
	 * 7.系统浏览器<br/>
	 * 验证是否支持直接启动访问指定页面的方法为：<br/>
	 * 执行下面的<code>doDefault()</code>时会出现如下选择框，<br/>
	 * 选择浏览器，如果能够正常访问，可以指定该浏览器访问指定网页；<br/>
	 * 如果该浏览器启动后没有跳转到指定网页，则不支持。<br/>
	 * 实践中，Go浏览器与天天浏览器并不支持。<br/>
	 * <img src="../../../../choice.png" mce_src="choice.png"/> <br/>
	 * 正确显示图片请在不改变包名的前提下将choice.png放于工程目录下，与src、res同级。<br/>
	 */
	private boolean letUserChoice = false;
	private String visitUrl = "http://blog.csdn.net/sodino";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// showUCBrowser();
		// showQQBrowser();
		// showOperaBrowser();
		// showDolphinBrowser();
		// showSkyfireBrowser();
		// showSteelBrowser();
		if (letUserChoice) {
			doDefault();
		} else {
			choiceBrowserToVisitUrl(visitUrl);
		}
		// 直接退出程序
		finish();
	}

	private void choiceBrowserToVisitUrl(String url) {
		boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle = false;
		String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", googlePath = "";
		PackageManager packageMgr = getPackageManager();
		List<PackageInfo> list = packageMgr.getInstalledPackages(0);
		for (int i = 0; i < list.size(); i++) {
			PackageInfo info = list.get(i);
			String temp = info.packageName;
			if (temp.equals("com.uc.browser")) {
				// 存在UC
				ucPath = temp;
				existUC = true;
			} else if (temp.equals("com.tencent.mtt")) {
				// 存在QQ
				qqPath = temp;
				existQQ = true;
			} else if (temp.equals("com.opera.mini.android")) {
				// 存在Opera
				operaPath = temp;
				existOpera = true;
			} else if (temp.equals("mobi.mgeek.TunnyBrowser")) {
				dolphinPath = temp;
				existDolphin = true;
			} else if (temp.equals("com.skyfire.browser")) {
				skyfirePath = temp;
				existSkyfire = true;
			} else if (temp.equals("com.kolbysoft.steel")) {
				steelPath = temp;
				existSteel = true;
			} else if (temp.equals("com.android.browser")) {
				// 存在GoogleBroser
				googlePath = temp;
				existGoogle = true;
			}
		}
		if (existUC) {
			gotoUrl(ucPath, url, packageMgr);
		} else if (existOpera) {
			gotoUrl(operaPath, url, packageMgr);
		} else if (existQQ) {
			gotoUrl(qqPath, url, packageMgr);
		} else if (existDolphin) {
			gotoUrl(dolphinPath, url, packageMgr);
		} else if (existSkyfire) {
			gotoUrl(skyfirePath, url, packageMgr);
		} else if (existSteel) {
			gotoUrl(steelPath, url, packageMgr);
		} else if (existGoogle) {
			gotoUrl(googlePath, url, packageMgr);
		} else {
			doDefault();
		}
	}

	private void gotoUrl(String packageName, String url, PackageManager packageMgr) {
		try {
			Intent intent;
			intent = packageMgr.getLaunchIntentForPackage(packageName);
			intent.setAction(Intent.ACTION_VIEW);
			intent.addCategory(Intent.CATEGORY_DEFAULT);
			intent.setData(Uri.parse(url));
			startActivity(intent);
		} catch (Exception e) {
			// 在1.5及以前版本会要求catch(android.content.pm.PackageManager.NameNotFoundException)异常，该异常在1.5以后版本已取消。
			e.printStackTrace();
		}
	}

	private void doDefault() {
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));
		startActivity(intent);
	}

	/** 直接启动UC，用于验证测试。 */
	private void showUCBrowser() {
		Intent intent = new Intent();
		intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(visitUrl));
		startActivity(intent);
	}

	/** 直接启动QQ，用于验证测试。 */
	private void showQQBrowser() {
		Intent intent = new Intent();
		intent.setClassName("com.tencent.mtt", "com.tencent.mtt.MainActivity");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(visitUrl));
		startActivity(intent);
	}

	/** 直接启动Opera，用于验证测试。 */
	private void showOperaBrowser() {
		Intent intent = new Intent();
		intent.setClassName("com.opera.mini.android", "com.opera.mini.android.Browser");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(visitUrl));
		startActivity(intent);
	}

	/** 直接启动Dolphin Browser，用于验证测试。 */
	private void showDolphinBrowser() {
		// 方法一：
		// Intent intent = new Intent();
		// intent.setClassName("mobi.mgeek.TunnyBrowser",
		// "mobi.mgeek.TunnyBrowser.BrowserActivity");
		// intent.setAction(Intent.ACTION_VIEW);
		// intent.addCategory(Intent.CATEGORY_DEFAULT);
		// intent.setData(Uri.parse(visitUrl));
		// startActivity(intent);
		// 方法二：
		gotoUrl("mobi.mgeek.TunnyBrowser", visitUrl, getPackageManager());
	}

	/** 直接启动Skyfire Browser，用于验证测试。 */
	private void showSkyfireBrowser() {
		// 方法一：
		Intent intent = new Intent();
		intent.setClassName("com.skyfire.browser", "com.skyfire.browser.core.Main");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(visitUrl));
		startActivity(intent);
		// 方法二：
		// gotoUrl("com.skyfire.browser", visitUrl, getPackageManager());
	}

	/** 直接启动Steel Browser，用于验证测试。 */
	private void showSteelBrowser() {
		// 方法一：
		// Intent intent = new Intent();
		// intent.setClassName("com.kolbysoft.steel",
		// "com.kolbysoft.steel.Steel");
		// intent.setAction(Intent.ACTION_VIEW);
		// intent.addCategory(Intent.CATEGORY_DEFAULT);
		// intent.setData(Uri.parse(visitUrl));
		// startActivity(intent);
		// 方法二：
		gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());
	}
	
	/**
	 * <activity android:theme="@style/BrowserTheme" android:label="@string/application_name" android:name="BrowserActivity" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation" android:alwaysRetainTaskState="true" android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:scheme="about" />
                <data android:scheme="javascript" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:scheme="inline" />
                <data android:mimeType="text/html" />
                <data android:mimeType="text/plain" />
                <data android:mimeType="application/xhtml+xml" />
                <data android:mimeType="application/vnd.wap.xhtml+xml" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.WEB_SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="" />
                <data android:scheme="http" />
                <data android:scheme="https" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
        </activity>
	 */
	
	
}